package com.miplus.ccrm.workflow.nodes.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.miplus.ccrm.workflow.model.enums.NodeType;
import com.miplus.ccrm.workflow.logic.LfEdge;
import com.miplus.ccrm.workflow.logic.LfNode;
import com.miplus.ccrm.workflow.nodes.BaseFlowConverter;
import com.miplus.ccrm.workflow.nodes.BaseV2NodeConverter;
import com.miplus.ccrm.workflow.nodes.IFlowConverter;
import com.miplus.ccrm.workflow.nodes.INodeConverter;

import java.util.List;
import java.util.Map;

public class ConditionNodeConverterImpl extends BaseV2NodeConverter implements INodeConverter {

    public String converter(Object config, List<Object> lines, List<Object> nodes, List<INodeConverter> nodeConverters, BaseFlowConverter baseFlowConverter) {
        return "";
    }

    @Override
    public String converter(LfNode config, List<LfEdge> lines, List<LfNode> nodes, List<INodeConverter> nodeConverters, IFlowConverter flowConverter) {
        String baseTemplate = super.converter(config, lines, nodes, nodeConverters, flowConverter);
        // 获取条件字段
        JSONArray branch = config.getProperties().getJSONObject("node_data").getJSONArray("branch");
        String[] branchBody = new String[branch.size()];
        for (int i = 0; i < branch.size(); i++) {
             // 获取每一个分支的条件

        }
        String[] body = new String[]{
                // 获取config里面设置的变量

        };


        return baseTemplate.replace("${body}",String.join("\n",body) );
    }

    @Override
    public NodeType getNodeType() {
        return NodeType.CONDITION;
    }

    private String getIfBody(JSONObject condition,int index){
        // 判断分支类型
        String joinStr = condition.getString("condition").equals("AND") ? "&&" : "||";
        String switchStr = condition.getString("type").replace(String.valueOf(index), "");
        JSONArray conditions = condition.getJSONArray("conditions");
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < conditions.size(); i++) {
            sb.append(getConditionItem(conditions.getJSONObject(i)));
        }
        return String.format("%s(%s){\n \n}",switchStr,sb);
    }


    private String getConditionItem(JSONObject conditionItem){
//        Map<String,String> conditionMap = Map.of(
//            "equals","%s == %s",
//            "not_equals","%s != %s",
//            "greater_than","%s > %s",
//            "less_than","%s < %s",
//            "greater_than_or_equal","%s >= %s",
//            "less_than_or_equal","%s <= %s",
//                "starts_with","%s.startsWith('%s')",
//                "ends_with","%s.endsWith('%s')"
//        );
        // java 8 不支持上面的写法,所以改成下面这样
        Map<String,String> conditionMap = new java.util.HashMap<>();
        conditionMap.put("equals","%s == %s");
        conditionMap.put("not_equals","%s != %s");
        conditionMap.put("greater_than","%s > %s");
        conditionMap.put("less_than","%s < %s");
        conditionMap.put("greater_than_or_equal","%s >= %s");
        conditionMap.put("less_than_or_equal","%s <= %s");
        conditionMap.put("starts_with","%s.startsWith('%s')");
        conditionMap.put("ends_with","%s.endsWith('%s')");
        return String.format(conditionMap.get(conditionItem.getString("compare")),conditionItem.getString("value"));
    }

}
